package club.mzywucai.blog.file_system.service.impl;

import club.mzywucai.blog.file_system.mapper.RelatedUploadMapper;
import club.mzywucai.blog.file_system.pojo.domain.JsonData;
import club.mzywucai.blog.file_system.pojo.domain.UploadResult;
import club.mzywucai.blog.file_system.pojo.po.FileInfoPo;
import club.mzywucai.blog.file_system.pojo.vo.UserVo;
import club.mzywucai.blog.file_system.service.RelatedUploadService;
import club.mzywucai.blog.file_system.util.MyStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mzywucai
 * @Description
 * @date 2019/2/10
 */
@Service
public class RelatedUploadServiceImpl implements RelatedUploadService {
    @Autowired
    RelatedUploadMapper relatedUploadMapper;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public JsonData updateUserPhotoURIByEmail(UserVo user, UploadResult result) {

        String email = user.getEmail();
        String uri = result.getUri();

        relatedUploadMapper.updateUserPhotoURIByEmail(uri, email);


        return JsonData.buildSuccess(0, "ok", "头像修改成功");
    }

    @Override
    public String searchFileInfoRemarkByIdAndLimitLevel1(String typeId) {
        boolean res = MyStringUtil.isNumericByJavaDefault(typeId);
        // 不全部是数字
        if (!res) {
            return "error";
        }

        long id = Long.parseLong(typeId);

        return relatedUploadMapper.searchFileInfoRemarkByIdAndLimitLevel1(id);
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void addFileInfo(FileInfoPo fileInfoPo) {

        relatedUploadMapper.addFileInfo(fileInfoPo);

    }
}
